#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

enum EDIR {
    UNDEFINED = 0,
    LEFT = 1,
    UP = 2,
    LEFT_UP = 3,
};

int m, n;
int X[1024];
int Y[1024];

int c[1024][1024];
EDIR b[1024][1024];

int resLen = 0;
int res[1024];

static void ReadInput() {
    cin >> m >> n;
    for (int i = 0; i < m; ++i)
        cin >> X[i];
    for (int i = 0; i < n; ++i)
        cin >> Y[i];
}

static void Solve() {
    for (int i = 0; i <= m; ++i) {
        c[i][0] = 0;
        b[i][0] = UNDEFINED;
    }

    for (int j = 0; j <= n; ++j) {
        c[0][j] = 0;
        b[0][j] = UNDEFINED;
    }

    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (X[i-1] == Y[j-1]) {
                c[i][j] = c[i-1][j-1] + 1;
                b[i][j] = LEFT_UP;
            } else if (c[i-1][j] >= c[i][j-1]) {
                c[i][j] = c[i-1][j];
                b[i][j] = UP;
            } else {
                c[i][j] = c[i][j-1];
                b[i][j] = LEFT;
            }
        }
    }

    int i = m, j = n;
    for (;i >= 0 && j >= 0;) {
        if (X[i-1] == Y[j-1]) {
            res[resLen] = X[i-1];
            ++resLen;
            --i;
            --j;
        } else if (b[i][j] == LEFT) {
            --i;
        } else {
            --j;
        }
    }
}

static void ShowResult() {
    cout << "----------------------------------" << endl
         << "c:" << endl;
    for (int i = 0; i <= m; ++i) {
        for (int j = 0; j <= n; ++j)
            cout << c[i][j] << "|" << b[i][j] << "\t";
        cout << endl;
    }
    cout << endl;

    cout << "----------------------------------" << endl
         << "resLen=" << resLen << endl
         << "res:" << endl;
    for (int i = resLen-1; i >= 0; --i)
        cout << res[i] << "\t";
    cout << endl;
}

int main() {
    ReadInput();
    Solve();
    ShowResult();
    return 0;
}
